<?php
/**
 * 后台视频管理的curd
 * Author: lyun
 * Date: 2018/1/26
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin;

use App\Http\Models\Video;
use App\Http\Models\VideoType;
use App\Http\Requests\Admin\VideoIndexRequest;
use App\Http\Requests\Admin\VideoStoreRequest;
use App\Http\Requests\Admin\VideoUpdateRequest;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class VideoController extends Controller
{
    use ApiResponser,ApiChecker;

    /**
     * Display a listing of the resource.
     *
     * @param VideoIndexRequest $request
     * @return \Illuminate\Http\Response
     */
    public function index(VideoIndexRequest $request)
    {
        $videos = Video::filterVideoTypeId($request->video_type_id)
            ->filterName($request->name)
            ->orderBy($request->order_by?:'created_at', $request->sort?:'desc')
            ->paginate($request->per_page?:15);

        return $this->response(compact('videos'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $video_types = VideoType::pluck('name', 'id')
            ->all();

        $upload_file_config = $this->getBosConfig();

        return $this->response(compact('video_types', 'upload_file_config'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  VideoStoreRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(VideoStoreRequest $request)
    {
        $video_data = $this->transformVideoData($request);

        $stored = Video::create($video_data);

        return $this->response(compact('stored'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  VideoUpdateRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(VideoUpdateRequest $request, $id)
    {
        /** @var Video $video */
        $video = Video::find($id);
            $this->checkItemExist($video, '视频');

        $video_data = $this->transformVideoData($request);
        unset($video_data['url']);

        $updated = $video->update($video_data);

        return $this->response(compact('updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        /** @var Video $video */
        $video = Video::find($id);
            $this->checkItemExist($video, '视频');

        $deleted = $video->delete();

        return $this->response(compact('deleted'));
    }

    /**
     * @param VideoStoreRequest|VideoUpdateRequest|Request $request
     * @return array
     */
    private function transformVideoData(Request $request)
    {
        return [
            'video_type_id' => $request->video_type_id,
            'name'          => $request->name,
            'url'           => $request->url,
            'preview_img'   => $request->preview_img,
            'sort'          => $request->sort?:0,
            'english_title' => $request->english_title,
        ];
    }

    /**
     *  返回bos的配置到前端，用来上传视频数据
     *
     * @return array
     */
    private function getBosConfig()
    {
        return [
            // bos bucket
            'prefix'          => 'videos/video/',
            'end_point'       => config('bos.config.endpoint'),
            'upload_max_size' => '990M',
            'bucket'          => config('bos.bucket'),
            'token_url'       => route('bos-token'),
        ];
    }
}
